package com.itmuch.youran.service.member;

import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itmuch.core.base.BaseService;
import com.itmuch.youran.domain.member.entity.Member;
import com.itmuch.youran.persistence.member.MemberMapper;
import com.itmuch.youran.util.StringUtil;

@Service
@Transactional(rollbackFor = Exception.class)
public class MemberService extends BaseService<MemberMapper, Member> {
	public Member findByKeys(String username, String email) {
		List<Member> list = this.dao.selectByKeys(username, email);
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	public Member loginByMobile(String mobile) {

		Member queryParam = new Member();
		queryParam.setMobile(mobile);

		Member member = this.dao.selectOne(queryParam);
		// 如果该手机号尚未注册, 则先自动注册
		if (member == null) {
			Member member2 = new Member();
			member2.setMobile(mobile);
			member2.setMobileBindFlag((byte) 1);

			member2.setPassword(StringUtil.radomString(64));
			member2.setUsername(UUID.randomUUID().toString());
			this.dao.insertSelective(member2);
			return member;
		}
		return member;
	}

	public Member findbyUsername(String username) {
		Member queryParam = new Member();
		queryParam.setUsername(username);

		return this.dao.selectOne(queryParam);
	}

}
